package Controllers.GenericToolsForController;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Date;

import javax.swing.JFrame;
import javax.swing.JOptionPane;

import Boundry.GenericTools.InputPanel;
import Boundry.GenericTools.ViewDataPanel;
import Controllers.GlobalData;
import Entity.Request;
import Entity.RequestStatusHistory;
import Entity.Worker;

public abstract class BaseClass implements ActionListener {

	public String[] columnNames=null;
	public String[][] tableData=null; 
	
	public void setListenerToViewDataPanel(ViewDataPanel panel)
	{
		panel.btnBack.addActionListener(this);
		panel.btnNext.addActionListener(this);
		panel.btnRefresh.addActionListener(this);
	}
	public void setListenerToInputPanel(InputPanel panel)
	{
		panel.btnBack.addActionListener(this);
		panel.btnSubnit.addActionListener(this);
		panel.btnAskForMoreTime.addActionListener(this);	
	}
	

	abstract public void actionPerformed(ActionEvent e); 
	
	/**
	 * if return -1 = the request is already Ask for more time, and the extension approve by inspector
	 * if return -2 = there is more then 3 days to this step
	 * if return 0 = there is a problem whit the server
	 * if return 1 = ask for more time send to inspector. 
	 * @param req
	 * @return
	 */
	public int askForMoreTime(Request req)
	{
	
		
		if(checkIfThereIsLessThenThreeDaysToCurrentStep(req)==false)
		{
			JOptionPane.showMessageDialog(new JFrame(),"You have more than 3 days to finish the step.\n Therefore, you cannot request for time extension!","Error",JOptionPane.ERROR_MESSAGE);
			return -2;
		}
		if(checkIfAlreadyExtensionApprove(req)==true)
		{
			JOptionPane.showMessageDialog(new JFrame(),"You have already got time extension.\n Therefore, you cannot request for time extension again!","Error",JOptionPane.ERROR_MESSAGE);
			return -1;
		}
		
		//Enter Reason for the extension 
		String reason = JOptionPane.showInputDialog(null, "Enter Reason for the extension : ", "Reason", 1);

		String[] possibilities = {"1", "2", "3","4","5","6","7"};
		String daysChoose = (String)JOptionPane.showInputDialog(
		                    null,
		                    "Choose number of days for extension:\n"
		                    + "You can Only Choose up to 7 days.",
		                    "",
		                    JOptionPane.PLAIN_MESSAGE,
		                    null,
		                    possibilities,
		                    "1");
		 int days = Integer.parseInt(daysChoose);
		
		 if(req.ackForMoreTime(days,reason,1) == true)
		 {	 
			 JOptionPane.showMessageDialog(new JFrame(),"Time extension was submitted!!\n Wait for improval.","",JOptionPane.INFORMATION_MESSAGE);
			 GlobalData.guiMangaer.switchScreen(GlobalData.mainScreen);
			 return 1;
		 }
		 else
		 {
			 JOptionPane.showMessageDialog(new JFrame(),"There is a Problem in Server, Try again later","Error",JOptionPane.ERROR_MESSAGE);
			 return 0;
		 }
	}
	/**
	 * 
	 * @param req
	 * @return
	 */
	private boolean checkIfThereIsLessThenThreeDaysToCurrentStep(Request req) 
	{
		Date now = new Date();
		Date dueDaye = req.getCurrentStatus().getDueDate();
		float dayLeft = (float) ((dueDaye.getTime() - now.getTime())/(1000.0*24.0*60.0*60.0));
		if(dayLeft <= 3 && dayLeft >=0)
			return true;
		return false;
	}
	/**
	 * Search history status the say that the request  is Already Extended
	 * @param req
	 * @return true - if the request is Already Extended
	 * 		   false - if the request is not Already Extended
	 */
	private boolean checkIfAlreadyExtensionApprove(Request req) 
	{
		String startWhit = req.getCurrentStatus().getStatus().getsStatus().toString().substring(0, 3); // get the first 3 letter of the step status
		String endWhit = "EXTENDED"; // if the current step is Already Extended, there is a step ststus that end whit "EXTENDED"
		for(RequestStatusHistory his : req.getHistory())
		{
			if(his.getStatus().getsStatus().toString().startsWith(startWhit) &&
					his.getStatus().getsStatus().toString().endsWith(endWhit) )
				return true;
		}
		return false;
	}
	

}
